<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=0.4"/>
<meta name="google" content="notranslate" />
<link rel="canonical" href="https://sleef.org/compile.xhtml" />
<link rel="icon" href="favicon.png" />
<link rel="stylesheet" type="text/css" href="texlike.css"/>
<link rel="stylesheet" type="text/css" href="sleef.css"/>
<title>SLEEF - Compiling and installing the library</title>
</head>
<body translate="no" class="notranslate">
<h1>SLEEF - Compiling and installing the library</h1>

<h2>Table of contents</h2>

<ul class="none" style="font-family: arial, sansserif; padding-left: 0.5cm;">
  <li><a class="underlined" href="index.xhtml">Introduction</a></li>
  <li>&nbsp;</li>
  <li><a class="underlined" href="compile.xhtml">Compiling and installing the library</a></li>
    <ul class="disc">
      <li><a href="#preliminaries">Preliminaries</a></li>
      <li><a href="#quickstart">Quick start</a></li>
      <li><a href="#linux">Compiling and installing the library on Linux</a></li>
      <li><a href="#MSVC">Compiling the library with Microsoft Visual C++</a></li>
      <li><a href="#hello">Compiling and running "Hello SLEEF"</a></li>
      <li><a href="#import">Importing SLEEF into your project</a></li>
      <li><a href="#cross">Cross compilation for Linux</a></li>
    </ul>
  <li>&nbsp;</li>
  <li><a class="underlined" href="purec.xhtml">Math library reference</a></li>
  <li><a class="underlined" href="quad.xhtml"> Quad-precision math library reference</a></li>
  <li><a class="underlined" href="dft.xhtml">DFT library reference</a></li>
  <li><a class="underlined" href="misc.xhtml">Other tools included in the package</a></li>
  <li><a class="underlined" href="benchmark.xhtml">Benchmark results</a></li>
  <li><a class="underlined" href="additional.xhtml">Additional notes</a></li>
</ul>

<h2 id="preliminaries">Preliminaries</h2>

<p class="noindent">
In order to build SLEEF, you need <a class="underlined"
href="http://www.cmake.org/">CMake</a>, which is an open-source and
cross-platform building tool. In order to test the library, it is
better to have <a class="underlined" href="http://www.mpfr.org/">the
GNU MPFR Library</a>, <a class="underlined"
href="https://wiki.openssl.org/index.php/Libssl_API">Libssl</a> and
<a class="underlined" href="http://www.fftw.org/">FFTW</a>.
</p>

<p>
CMake works by allowing the developer to specify build parameters and
rules in a simple text file that cmake then processes to generate
project files for the actual native build tools (e.g. UNIX Makefiles,
Microsoft Visual Studio, Apple XCode, etc). If you are not already
familiar with cmake, please refer to the
<a class="underlined" href="https://cmake.org/documentation/">official documentation</a>
or
the <a class="underlined" href="https://gitlab.kitware.com/cmake/community/-/wikis/home">basic
introductions in the wiki</a>.
</p>

<h2 id="quickstart">Quick start</h2>

<p class="noindent">
You will find quick start instructions in the sources or via GitHub in the
<a class="underlined" href="https://github.com/shibatch/sleef/blob/master/README.adoc">README.adoc</a>
file.
</p>

<h2 id="linux">Compiling and installing the library on Linux</h2>

<p class="noindent">
  In order to build the library, you may want to install OpenMP (optional).
  In order to test the library, you need to install libmpfr, libssl and
  libfftw3 (optional). Availability of these libraries are checked upon
  execution of cmake.
  Please change the directory to sleef-3.X and run the following commands.

<pre class="command">$ sudo apt-get install libmpfr-dev libssl-dev libfftw3-dev
$ cmake -S . -B build/ -DCMAKE_INSTALL_PREFIX=./install
$ cmake --build build/ --clean-first -j `nproc`
$ ctest --test-dir build/ -j `nproc`
$ cmake --install build/
</pre>
</p>

<p>
  In order to uninstall the libraries and headers, run the following command.
</p>

<pre class="command" style="margin-top: 1em;">$ sudo xargs rm -v &lt; build/install_manifest.txt</pre>

<h3 id="lto">Building the library with LTO support</h3>

<p class="noindent">
  You can build the library with <a class="underlined"
  href="additional.xhtml#lto">link time opimization(LTO)</a> support
  with the following commands. Note that you can only build static
  libraries with LTO support. You also have to use the same compiler
  with the same version to build the library and other source codes.
</p>

<pre class="command">$ cmake -S . -B build/ -DCMAKE_INSTALL_PREFIX=./install -DSLEEF_ENABLE_LTO=TRUE</pre>

<p>
  In order to build the library with thinLTO support with clang, you
  need to specify LLVM AR command that exactly corresponds to the
  clang compiler.
</p>

<pre class="command">$ CC=clang-19 CXX=clang++-19 cmake -S . -B build/ -DCMAKE_INSTALL_PREFIX=./install -DSLEEF_ENABLE_LTO=TRUE -DSLEEF_LLVM_AR_COMMAND=llvm-ar-19 -DCMAKE_EXE_LINKER_FLAGS=-fuse-ld=lld-19</pre>


<h3 id="inline">Building the header files for inlining the whole SLEEF functions</h3>

<p class="noindent">
  <a class="underlined" href="additional.xhtml#inline">Header files
  for inlining the whole SLEEF functions</a> can be built with the
  following commands. With these header files, it may be easier to
  inline the whole SLEEF functions than using LTO. You have to specify
  "-ffp-contract=off" compiler option when compiling a source code
  that includes one of these header files.
</p>

<pre class="command">$ cmake -S . -B build/ -DCMAKE_INSTALL_PREFIX=./install -DSLEEF_BUILD_INLINE_HEADERS=TRUE</pre>

<h2 id="MSVC">Compiling the library with Microsoft Visual C++</h2>

<p class="noindent">
  Open developer command prompt for VS2022 and change directory to
  sleef-3.X. You can use the accompanying batch file named
  winbuild-msvc.bat to build the library. The arguments of this batch
  file are passed to cmake.
</p>

<p>
  Below is an example of commands for building SLEEF with Visual
  Studio.
</p>

<pre class="command">D:\sleef-3.X> winbuild-msvc -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="%CD%/install"
</pre>


<h3 id="cow">Compiling the library with Clang on Windows</h3>

<p class="noindent">
  Install ninja and clang via VS2022 installer.  Below is an example
  of commands for building SLEEF with Clang on Windows.
</p>

<pre class="command">D:\sleef-3.X> winbuild-clang -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="%CD%/install"
</pre>

<p>
  You can choose your own installation of LLVM executable by setting
  CLANGINSTALLDIR environment variable.
</p>

<pre class="command">D:\sleef-3.X> set CLANGINSTALLDIR=C:\Program Files\LLVM
D:\sleef-3.X> winbuild-clang -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="%CD%/install"
</pre>


<h2 id="hello">Compiling and running "Hello SLEEF"</h2>

<p class="noindent">
  Now, let's try compiling the <a class="underlined"
  href="hellox86.c">source code shown in Fig. 2.1</a>.
</p>

<pre class="code">
<code>#include &lt;stdio.h&gt;</code>
<code>#include &lt;x86intrin.h&gt;</code>
<code>#include &lt;sleef.h&gt;</code>
<code></code>
<code>int main(int argc, char **argv) {</code>
<code>  double a[] = {2, 10};</code>
<code>  double b[] = {3, 20};</code>
<code></code>
<code>  __m128d va, vb, vc;</code>
<code>  </code>
<code>  va = _mm_loadu_pd(a);</code>
<code>  vb = _mm_loadu_pd(b);</code>
<code></code>
<code>  vc = Sleef_powd2_u10(va, vb);</code>
<code></code>
<code>  double c[2];</code>
<code></code>
<code>  _mm_storeu_pd(c, vc);</code>
<code></code>
<code>  printf("pow(%g, %g) = %g\n", a[0], b[0], c[0]);</code>
<code>  printf("pow(%g, %g) = %g\n", a[1], b[1], c[1]);</code>
<code>}</code>
</pre>
<p style="text-align:center;">
  Fig. 2.1: <a href="hellox86.c">Source code for testing</a>
</p>



<p style="margin-top: 2cm;">
  Fig.2.2 shows typical commands for compiling and executing the hello
  code on Linux computers.
</p>

<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ gcc docs/src/hellox86.c -o hellox86 -L./install/lib -I./install/include -lsleef -ltlfloat
$ ./hellox86
pow(2, 3) = 8
pow(10, 20) = 1e+20
$ &block;</pre>
<p style="text-align:center;">
  Fig. 2.2: Commands for compiling and executing hellox86.c
</p>

<p style="margin-top: 2cm;">
  You may need to set LD_LIBRARY_PATH environment variable
  appropriately. If you are trying to execute the program on Mac OSX
  or Windows, try copying the DLLs to the current directory.
</p>

<h2 id="import">Importing SLEEF into your project</h2>


<p class="noindent">
  Below is an example <a class="underlined"
  href="CMakeLists.txt">CMakeLists.txt</a> for compiling the above
  hellox86.c. CMake will automatically download SLEEF from GitHub
  repository, and thus there is no need to include SLEEF in your
  software package. If you prefer importing SLEEF as a submodule in
  git, you can use SOURCE_DIR option instead of GIT_REPOSITORY option
  for ExternalProject_Add.
</p>

<pre class="code">
<code>cmake_minimum_required(VERSION 3.5.1)</code>
<code>include(ExternalProject)</code>
<code>find_package(Git REQUIRED)</code>
<code></code>
<code>ExternalProject_Add(libsleef</code>
<code>  GIT_REPOSITORY https://github.com/shibatch/sleef</code>
<code>  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/contrib</code>
<code>)</code>
<code></code>
<code>include_directories(${CMAKE_BINARY_DIR}/contrib/include)</code>
<code>link_directories(${CMAKE_BINARY_DIR}/contrib/lib)</code>
<code></code>
<code>add_executable(hellox86 hellox86.c)</code>
<code>add_dependencies(hellox86 libsleef)</code>
<code>target_link_libraries(hellox86 sleef)</code>
</pre>
<p style="text-align:center;">
  Fig. 2.3: <a href="CMakeLists.txt">Example CMakeLists.txt</a>
</p>


<h2 id="cross">Cross compilation for Linux</h2>

<p>
Below, we explain the method for cross-compiling SLEEF. It relies on
the toolchain files provided under toolchains directory. Below is an
example of cross-compiling SLEEF for S390X architecrure on a platform
with x86_64 Linux.
</p>

<p>
Please run the following from the root directory of SLEEF:
</p>

<br/>

<p>
1. First, compile the native SLEEF.
</p>

<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ cmake -S . -B build-native/ -DCMAKE_INSTALL_PREFIX=./install-native
$ cmake --build build-native -j --clean-first
</pre>

<p>
2. Cross-compile the target platform's SLEEF.
</p>

<pre class="command" style="margin-top: 2.0em; margin-bottom: 0.5cm;">$ cmake -DCMAKE_TOOLCHAIN_FILE=./toolchains/s390x-gcc.cmake -DNATIVE_BUILD_DIR=$(pwd)/build-native/ -DCMAKE_INSTALL_PREFIX=./install-cross -S . -B build-cross
$ cmake --build build-cross -j --clean-first
</pre>


<p class="footer">
  Copyright &copy; 2010-2025 SLEEF Project, Naoki Shibata and contributors.<br/>
  SLEEF is open-source software and is distributed under the Boost Software License, Version 1.0.
</p>

<script type="text/javascript">
var sc_project=13098265; 
var sc_invisible=1; 
var sc_security="518de45e"; 
</script>
<script type="text/javascript"
src="https://www.statcounter.com/counter/counter.js"
async="async"></script>
<noscript><div class="statcounter"><a title="Web Analytics"
href="https://statcounter.com/" target="_blank"><img
class="statcounter"
src="https://c.statcounter.com/13098265/0/518de45e/1/"
alt="Web Analytics"
referrerPolicy="no-referrer-when-downgrade"></img></a></div></noscript>


</body>
</html>
